package org.miradi.diagram.coalescer;

import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class GroupManager
{
	
	public static void reset()
	{
		groups.clear();
	}

	public static Set<Group> getGroups()
	{
		return groups;
	}

	public static boolean maybeNewGroup(int direction, Set<Node> nodes)
	{
		return nodes.size() > 1 && groups.add(new Group(direction, nodes));
	}
	
	public static boolean groupsExist()
	{
		return null!=groups && !groups.isEmpty();
	}

	public static Group dequeueLargestGroup()
	{
		if (null==groups || groups.isEmpty()) 
			return null;
				
		SortedSet<Group> treeSet = new TreeSet<Group>(groups);
		Group largest = treeSet.last();
		groups.remove(largest);
			
		return largest;
	}
	
	public static void collectGroups()
	{
		for (Node node : NodeManager.getNodes()) 
		{
			if (node.isGrouped()) 
			{
				groups.add(node.getGroup());
			}
		}		
	}

	private static final Set<Group> groups = new HashSet<Group>();

}
